import { createPinia } from 'pinia'

const PREFIX = "pinia::"
const pinia = createPinia();

export function persistenceStorke (context) {
  
  let store = context.store;
  //存
  window.addEventListener("beforeunload", e => {
    //转json
    localStorage.setItem(PREFIX+store.$id,JSON.stringify(store.$state))
  })
  // 取
  try {
    const data = localStorage.getItem(PREFIX + store.$id)
    // store.$patch(JSON.parse(data))
  } catch (e) {
    throw e;
  }
}

  // 注册插件
  pinia.use(persistenceStorke)
export default pinia